import Vue from 'vue';
import AxiosPlugin from './js/lib/axiosPlugin';
import API from './js/interface/index';
import { getLogin } from "./js/lib/utils";
import './js/lib/vantPlugin';

import Router from 'vue-router';
import routes from './router';
import App from './App';

import './assets/css/reset.scss';
import 'amfe-flexible';
// import 'lib-flexible';

Vue.use(AxiosPlugin);
// Vue.use(wechatShare);

// Vue.prototype.wechatShare  = wechatShare;
Vue.config.productionTip = false;

Vue.use(Router);

const router = new Router({
	// mode: 'history',
    routes
});

window.API = API;//把引入统一管理接口页面挂在全局对象window.API下面

router.beforeEach(async (to, from, next) => {
   let token = localStorage.getItem('token');
   if(!token) {
        let code = to.query['code'] ? to.query['code'] : '';
        if(code) {
            let result = await getLogin(code);
            if(result) {
                let data = result.data.userinfo;
                localStorage.setItem('token', data.token);
                next();
            }else {
                //防止code过期失效
                window.location.href = window.location.origin + to.path;
            }
        }else {
            let pageUrl = window.location.href;
            let wxurl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + window.config['AppId'] + "&redirect_uri=" + encodeURIComponent(pageUrl) + "&response_type=code&scope=snsapi_base&state=wangfei#wechat_redirect";
            window.location.href = wxurl;
        }

   }else {
        //当前有本地缓存
        next();
   }
});

/*
router.afterEach((to, from) => {
    let authUrl = `${window.location.origin}${to.fullPath}`;
    let allowShare = !!to.meta.allowShare;

    if (!!window.__wxjs_is_wkwebview) {// IOS
        if (window.entryUrl == "" || window.entryUrl == undefined) {
            window.entryUrl = authUrl; // 将后面的参数去除
        }
        wechatAuth(authUrl, "ios", allowShare);
    } else {
        // 安卓
        setTimeout(function () {
            wechatAuth(authUrl, "android", allowShare);
        }, 500);
    }
});
*/

new Vue({
    el: '#app',
    router,
    render: h => h(App)
});